import 'package:event_bus/event_bus.dart';
import 'package:flutter/material.dart';
import 'package:flutter_rider/api/api.dart';
import 'package:flutter_rider/event/action_home_index_event.dart';
import 'package:flutter_rider/main.dart';
import 'package:flutter_rider/main/widgets/ext/u_layout.dart';
import 'package:flutter_rider/main/widgets/ext/u_text.dart';
import 'package:flutter_rider/main/widgets/ui_constant.dart';
import 'package:flutter_rider/module/mine/models/msg_record_model.dart';
import 'package:flutter_rider/module/task/task_router.dart';
import 'package:flutter_rider/module/waybill/waybill_router.dart';
import 'package:flutter_rider/routers/fluro_navigator.dart';
import 'package:flutter_rider/routers/routers.dart';
import 'package:flutter_rider/utils/screen_fit.dart';
import 'package:flutter_rider/utils/tip.dart';

class MsgWidget extends StatelessWidget {
  final MsgRecordModel msg;

  const MsgWidget(this.msg, {Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    //车辆 X5672 已停止2分钟，请及时处理 >>
    var title = "";
    var desStart = "";
    var desMiddle = "";
    var desEnd = "";
    switch (msg.name_type) {
      case 0:
        title = "新运单";
        desStart = "运单 ";
        desMiddle = msg.waybill_no ?? "";
        desEnd = " 下单成功，请及时处理>>";
        break;
      case 1:
        title = "运单已取消";
        desStart = "运单 ";
        desMiddle = msg.waybill_no ?? "";
        desEnd = " 已取消，请及时处理>>";
        break;
      case 2:
        title = "新任务";
        desStart = "任务 ";
        desMiddle = msg.waybill_mission_no ?? "";
        desEnd = " 已生成，请及时处理>>";
        break;
      case 3:
        title = "到站消息(装货站点)";
        desStart = "车辆 ";
        desMiddle = msg.car_no ?? "";
        desEnd = " 已到达装货站点，请及时装货>>";
        break;
      case 4:
        title = "到站消息(卸货货站点)";
        desStart = "车辆 ";
        desMiddle = msg.car_no ?? "";
        desEnd = " 已到达装货站点，请及时卸货>>";
        break;
      case 5:
        title = "预警消息";
        desStart = "车辆 ";
        desMiddle = msg.car_no ?? "";
        desEnd = " 已停止2分钟，请及时处理 >>";
        break;
    }

    return GestureDetector(
      onTap: () {
        MsgWidget.actionRouter(context, msg);
      },
      child: Container(
          color: UIColor.color_content,
          // decoration: BoxDecoration(
          //   color: UIColor.color_content,
          //   borderRadius: BorderRadius.all(Radius.circular(8.rpx)),
          // ),
          // margin: EdgeInsets.only(top: 12.rpx, left: 12.rpx, right: 12.rpx),
          child: Stack(children: [
            Column(
              children: [
                Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    UText.get(
                      title,
                      UIColor.color_t16,
                      16.rpx,
                    ).padding(EdgeInsets.only(left: 12.rpx, top: 12.rpx)),
                    const Spacer(),
                    UText.get(msg.created_time, UIColor.color_t17, 12.rpx)
                        .padding(EdgeInsets.only(right: 12.rpx, top: 14.rpx)),
                  ],
                ),
                Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    RichText(
                      text: TextSpan(
                        text: desStart,
                        style: TextStyle(
                          color: UIColor.color_t3,
                          fontSize: 12.rpx,
                        ),
                        children: [
                          TextSpan(
                              text: desMiddle,
                              style: TextStyle(
                                  color: UIColor.color_t16,
                                  fontSize: 12.rpx,
                                  fontWeight: FontWeight.w600)),
                          TextSpan(
                              text: desEnd,
                              style: TextStyle(
                                color: UIColor.color_t16,
                                fontSize: 12.rpx,
                              )),
                        ],
                      ),
                    )
                        .padding(EdgeInsets.only(
                            left: 12.rpx,
                            top: 4.rpx,
                            bottom: 12.rpx,
                            right: (6 + 10 + 12).rpx))
                        .expand,
                  ],
                ),
                0.5.hLine16
              ],
            ),
            // Positioned(
            //     bottom: 12.rpx,
            //     right: 12.rpx,
            //     child: Container(
            //       width: 10.rpx,
            //       height: 10.rpx,
            //       //color: UIColor.color_none,
            //       decoration: BoxDecoration(
            //           color: UIColor.color_none,
            //           borderRadius: BorderRadius.circular(5.rpx)),
            //     )),
          ])),
    );
  }

  static actionRouter(BuildContext context, MsgRecordModel msg) {
    switch (msg.name_type) {
      case 0:
      case 1:
        WaybillRouter.openWaybillDetail(context, msg.waybill_no ?? "");
        break;
      case 2:
      case 3:
      case 4:
        NavigatorUtils.push(context, TaskRouter.taskDetailPage,
            arguments: msg.waybill_mission_id.toString());
        break;
      // case 3:
      // TaskRouter.openTaskLoadDetail(
      //     context, msg.waybill_mission_id.toString());
      // NavigatorUtils.push(context, TaskRouter.orderLoadPage,
      //     arguments: msg.waybill_mission_id.toString());
      // break;
      // case 4:
      // TaskRouter.openTaskUnloadDetail(
      //     context, msg.waybill_mission_id.toString());
      // NavigatorUtils.push(context, TaskRouter.orderUnloadPage,
      //     arguments: msg.waybill_mission_id.toString());
      // break;
      case 5:
        NavigatorUtils.push(context, Routes.homePage, clearStack: true);
        Future.delayed(const Duration(milliseconds: 100), () {
          eventBus.fire(ActionHomeIndexEvent(3)); //延迟下，等首页初始化完毕
        });

        break;
    }
  }
}

class MsgSubWidget extends StatelessWidget {
  final MsgRecordModel msg;

  const MsgSubWidget(this.msg, {Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    //车辆 X5672 已停止2分钟，请及时处理 >>
    var title = "";
    var desStart = "";
    var desMiddle = "";
    var desEnd = "";
    switch (msg.name_type) {
      case 0:
        title = "新运单";
        desStart = "运单 ";
        desMiddle = msg.waybill_no ?? "";
        desEnd = " 下单成功，请及时处理>>";
        break;
      case 1:
        title = "运单已取消";
        desStart = "运单 ";
        desMiddle = msg.waybill_no ?? "";
        desEnd = " 已取消，请及时处理>>";
        break;
      case 2:
        title = "新任务";
        desStart = "任务 ";
        desMiddle = msg.waybill_mission_no ?? "";
        desEnd = " 已生成，请及时处理>>";
        break;
      case 3:
        title = "到站消息(装货站点)";
        desStart = "车辆 ";
        desMiddle = msg.car_no ?? "";
        desEnd = " 已到达装货站点，请及时装货>>";
        break;
      case 4:
        title = "到站消息(卸货货站点)";
        desStart = "车辆 ";
        desMiddle = msg.car_no ?? "";
        desEnd = " 已到达装货站点，请及时卸货>>";
        break;
      case 5:
        title = "预警消息";
        desStart = "车辆 ";
        desMiddle = msg.car_no ?? "";
        desEnd = " 已停止2分钟，请及时处理 >>";
        break;
    }

    return GestureDetector(
        onTap: () {
          MsgWidget.actionRouter(context, msg);
        },
        child: Column(children: [
          Container(
              decoration: BoxDecoration(
                color: UIColor.color_content,
                borderRadius: BorderRadius.all(Radius.circular(8.rpx)),
              ),
              // margin: EdgeInsets.only(top: 12.rpx, left: 12.rpx, right: 12.rpx),
              child: Stack(children: [
                Column(
                  children: [
                    Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        UText.get(
                          title,
                          UIColor.color_t16,
                          16.rpx,
                        ).padding(EdgeInsets.only(left: 12.rpx, top: 12.rpx)),
                        const Spacer(),
                        UText.get(msg.created_time, UIColor.color_t17, 12.rpx)
                            .padding(
                                EdgeInsets.only(right: 12.rpx, top: 14.rpx)),
                      ],
                    ),
                    Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        RichText(
                          text: TextSpan(
                            text: desStart,
                            style: TextStyle(
                              color: UIColor.color_t3,
                              fontSize: 12.rpx,
                            ),
                            children: [
                              TextSpan(
                                  text: desMiddle,
                                  style: TextStyle(
                                      color: UIColor.color_t16,
                                      fontSize: 12.rpx,
                                      fontWeight: FontWeight.w600)),
                              TextSpan(
                                  text: desEnd,
                                  style: TextStyle(
                                    color: UIColor.color_t16,
                                    fontSize: 12.rpx,
                                  )),
                            ],
                          ),
                        )
                            .padding(EdgeInsets.only(
                                left: 12.rpx,
                                top: 4.rpx,
                                bottom: 12.rpx,
                                right: (6 + 10 + 12).rpx))
                            .expand,
                      ],
                    ),
                  ],
                ),
                // Positioned(
                //     bottom: 12.rpx,
                //     right: 12.rpx,
                //     child: Container(
                //       width: 10.rpx,
                //       height: 10.rpx,
                //       //color: UIColor.color_none,
                //       decoration: BoxDecoration(
                //           color: UIColor.color_none,
                //           borderRadius: BorderRadius.circular(5.rpx)),
                //     )),
              ])),
          10.hspace
        ]));
  }
}
